/**
 * 
 */
package com.zxc.serialization.protobuf;

import java.io.IOException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.protobuf.InvalidProtocolBufferException;
import com.zxc.utils.GzipUtil;

import jodd.util.Base64;

/**
 * @author admin
 *
 */
public class ProtobufTest {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
//		testTravelInfo();
		testInnerData();
	}
	
	
	static void testInnerData() throws IOException{
		String s = "{\"routecodes\":\"HKG-PEK,PEK-LAX,LAX-SFO,SFO-HKG;CA,AA,AA,AA;CA6606,AA180,AA6007,AA8931;2017-11-24 12:00:00/2017-11-24 15:20:00,2017-11-24 18:30:00/2017-11-24 15:15:00,2017-12-24 18:30:00/2017-12-24 19:55:00,2017-12-24 22:50:00/2017-12-26 06:00:00;V,N,Q,Q\",\"dsid\":\"ABUS_F-164\",\"travelInfo\":{\"accountcode\":\"\",\"adjustprice\":0,\"basic\":\"ABUS_F-164#164#Economy#E#0^ ^ ^CA^CX^6606^2017-11-24 12:00:00^2017-11-24 15:20:00^9^200^HKG^PEK^333^ ^V^ ^190^0^ ^true^1^3^8^8^O@^0^ ^ ^AA^AA^180^2017-11-24 18:30:00^2017-11-24 15:15:00^9^765^PEK^LAX^788^ ^N^DS^175^0^ ^false^2^B^8^-8^O@^1^ ^ ^AA^AA^6007^2017-12-24 18:30:00^2017-12-24 19:55:00^9^85^LAX^SFO^E75^ ^Q^ ^175^0^ ^false^0^2^-8^-8^O@^1^ ^ ^AA^CX^8931^2017-12-24 22:50:00^2017-12-26 06:00:00^1^910^SFO^HKG^77W^ ^Q^BD^0^0^ ^true^I^1^-8^8^I#4910^2191^1^ADT^HKD^0^0,1-2P;2,3-2P##HKG-PEK,PEK-LAX,LAX-SFO,SFO-HKG;CA,AA,AA,AA;CA6606,AA180,AA6007,AA8931;2017-11-24 12:00:00/2017-11-24 15:20:00,2017-11-24 18:30:00/2017-11-24 15:15:00,2017-12-24 18:30:00/2017-12-24 19:55:00,2017-12-24 22:50:00/2017-12-26 06:00:00;V,N,Q,Q#private#roundTrip#ABUS_F#HKG-LAX#AA#7101#4910#7101#4910#0#3YCD#NLE08QN1%2FHK07,NLE08QN1%2FHK07;QLECX2O3%2FHK07,QLECX2O3%2FHK07#AA#private,private;private,private#\",\"ds\":\"ABUS_F\",\"dsid\":\"ABUS_F-164\",\"farebasis\":\"NLE08QN1%2FHK07,NLE08QN1%2FHK07;QLECX2O3%2FHK07,QLECX2O3%2FHK07\",\"faretype\":\"private,private;private,private\",\"flightClass\":\"Economy\",\"flightClassCode\":\"E\",\"flightOption\":\"roundTrip\",\"flightSegments\":[{\"arrivalAirport\":\"PEK\",\"arrivalCityCode\":\" \",\"arrivalTime\":\"2017-11-24 15:20:00\",\"arrivalTimeZone\":\"8\",\"bookingCode\":\"V\",\"departAirport\":\"HKG\",\"departCityCode\":\" \",\"departureTime\":\"2017-11-24 12:00:00\",\"departureTimeZone\":\"8\",\"eQuipType\":\"333\",\"eTicket\":\" \",\"elapseTime\":200,\"flightNumber\":\"6606\",\"fromterminal\":\"1\",\"jingting\":\" \",\"marketingCarrier\":\"CA\",\"marriageGrp\":\"O\",\"meal\":\" \",\"operatingCarrier\":\"CX\",\"originDestinationOption\":\"0\",\"seatsRemain\":9,\"sharecode\":true,\"stayTime\":190,\"stopQuantity\":\"0\",\"toterminal\":\"3\"},{\"arrivalAirport\":\"LAX\",\"arrivalCityCode\":\" \",\"arrivalTime\":\"2017-11-24 15:15:00\",\"arrivalTimeZone\":\"-8\",\"bookingCode\":\"N\",\"departAirport\":\"PEK\",\"departCityCode\":\" \",\"departureTime\":\"2017-11-24 18:30:00\",\"departureTimeZone\":\"8\",\"eQuipType\":\"788\",\"eTicket\":\" \",\"elapseTime\":765,\"flightNumber\":\"180\",\"fromterminal\":\"2\",\"jingting\":\" \",\"marketingCarrier\":\"AA\",\"marriageGrp\":\"O\",\"meal\":\"DS\",\"operatingCarrier\":\"AA\",\"originDestinationOption\":\"0\",\"seatsRemain\":9,\"sharecode\":false,\"stayTime\":175,\"stopQuantity\":\"0\",\"toterminal\":\"B\"},{\"arrivalAirport\":\"SFO\",\"arrivalCityCode\":\" \",\"arrivalTime\":\"2017-12-24 19:55:00\",\"arrivalTimeZone\":\"-8\",\"bookingCode\":\"Q\",\"departAirport\":\"LAX\",\"departCityCode\":\" \",\"departureTime\":\"2017-12-24 18:30:00\",\"departureTimeZone\":\"-8\",\"eQuipType\":\"E75\",\"eTicket\":\" \",\"elapseTime\":85,\"flightNumber\":\"6007\",\"fromterminal\":\"0\",\"jingting\":\" \",\"marketingCarrier\":\"AA\",\"marriageGrp\":\"O\",\"meal\":\" \",\"operatingCarrier\":\"AA\",\"originDestinationOption\":\"1\",\"seatsRemain\":9,\"sharecode\":false,\"stayTime\":175,\"stopQuantity\":\"0\",\"toterminal\":\"2\"},{\"arrivalAirport\":\"HKG\",\"arrivalCityCode\":\" \",\"arrivalTime\":\"2017-12-26 06:00:00\",\"arrivalTimeZone\":\"8\",\"bookingCode\":\"Q\",\"departAirport\":\"SFO\",\"departCityCode\":\" \",\"departureTime\":\"2017-12-24 22:50:00\",\"departureTimeZone\":\"-8\",\"eQuipType\":\"77W\",\"eTicket\":\" \",\"elapseTime\":910,\"flightNumber\":\"8931\",\"fromterminal\":\"I\",\"jingting\":\" \",\"marketingCarrier\":\"AA\",\"marriageGrp\":\"I\",\"meal\":\"BD\",\"operatingCarrier\":\"CX\",\"originDestinationOption\":\"1\",\"seatsRemain\":1,\"sharecode\":true,\"stayTime\":0,\"stopQuantity\":\"0\",\"toterminal\":\"1\"}],\"flightcode\":\"AA\",\"fromto\":\"HKG-LAX\",\"infoSource\":\"private\",\"itineraryID\":\"164\",\"marriageGrps\":\"O,O,O,I\",\"policy\":\"\",\"price\":7101.0,\"price2\":7101.0,\"priceInfo\":[{\"afterRebateFare\":4189,\"baggage\":\"0,1-2P;2,3-2P\",\"baseFare\":4910.0,\"bizFirmAmount\":0,\"bizPercentAmount\":0,\"cidAdjustAmt\":0,\"currencyCode\":\"HKD\",\"fareExchangeRate\":0.853,\"firmAmountCid\":0,\"firmAmountHx\":0,\"keepDigit\":0,\"lastRmbFare\":4189,\"lastTaxFare\":1869,\"openAutoAdjustTax\":0,\"passengerTypeName\":\"ADT\",\"percentAmountCid\":0,\"percentAmountHx\":0,\"policyId\":\"0\",\"priceType\":0,\"quantity\":1,\"rebateAmount\":0.0,\"rebateCurrency\":\"CNY\",\"rebateExchangeRate\":1.0,\"rebatePer\":0.0,\"rmbFare\":4188.2300,\"rmbRebateAmount\":0.0000,\"rmbTax\":1868.9230,\"rmbTicketPrice\":0.0000,\"segmentPrice\":0.0,\"serviceFee\":0.0,\"serviceFeeRate\":1.0,\"serviceFeeRmb\":0,\"tax\":2191.0,\"taxFirmAmount\":0,\"taxPercentAmount\":0,\"ticketPrice\":0.0,\"ticketPriceRule\":\"0\",\"ticketSettleRule\":\"1\"}],\"pricej\":{\"jsRatio\":1.0,\"adultTax\":1869.0,\"childTax\":0.0,\"adultPrice\":4189.0,\"localCurrencyCode\":\"HKD\",\"childPrice\":0.0,\"priceAddInfo\":\"\",\"rmbRatio\":0.853},\"productType\":\"default\",\"qsource\":\"3YCD\",\"routecode\":\"HKG-PEK,PEK-LAX,LAX-SFO,SFO-HKG;CA,AA,AA,AA;CA6606,AA180,AA6007,AA8931;2017-11-24 12:00:00/2017-11-24 15:20:00,2017-11-24 18:30:00/2017-11-24 15:15:00,2017-12-24 18:30:00/2017-12-24 19:55:00,2017-12-24 22:50:00/2017-12-26 06:00:00;V,N,Q,Q\",\"valid\":true,\"validatingcarrier\":\"AA\",\"ywprice\":4910.0,\"ywprice2\":4910.0},\"vendorId\":\"600058\",\"index\":146,\"queryResult\":{\"applyType\":\"0\",\"adultTax\":1869,\"suitAge\":\"\",\"data\":\"10282285985426630_146\",\"childTax\":1869,\"adultPrice\":4189,\"priceType\":\"0\",\"rule\":{\"specialNoShow\":0,\"partEndorse\":0,\"endorsement\":0,\"baggage\":\"去程：2件/回程：2件\",\"penalty\":\"100%\",\"hasRefund\":0,\"hasBaggage\":1,\"endorse\":\"\",\"hasNoShow\":0,\"hasEndorse\":0,\"noShowLimitTime\":0,\"partRefund\":0,\"refund\":\"100%-0-*\"},\"childTaxType\":\"0\",\"gvChildRule\":\"1\",\"minPassengerCount\":1,\"fromSegments\":[{\"operatingFlightNo\":\"\",\"cabinClass\":\"Y\",\"stopCities\":\"\",\"baggage\":{\"pc\":2,\"weight\":23,\"isAllWeight\":true},\"depTime\":\"201711241200\",\"cabin\":\"V\",\"depAirport\":\"HKG\",\"seatCount\":\"9\",\"flightNumber\":\"CA6606\",\"carrier\":\"CA\",\"codeShare\":true,\"aircraftCode\":\"333\",\"arrAirport\":\"PEK\",\"operatingCarrier\":\"CX\",\"arrTime\":\"201711241520\"},{\"operatingFlightNo\":\"\",\"cabinClass\":\"Y\",\"stopCities\":\"\",\"baggage\":{\"pc\":2,\"weight\":23,\"isAllWeight\":true},\"depTime\":\"201711241830\",\"cabin\":\"N\",\"depAirport\":\"PEK\",\"seatCount\":\"9\",\"flightNumber\":\"AA180\",\"carrier\":\"AA\",\"codeShare\":false,\"aircraftCode\":\"788\",\"arrAirport\":\"LAX\",\"operatingCarrier\":\"AA\",\"arrTime\":\"201711241515\"}],\"nationality\":\"\",\"maxPassengerCount\":9,\"nationalityType\":\"0\",\"invoiceType\":3,\"adultTaxType\":\"0\",\"childPrice\":4189,\"retSegments\":[{\"operatingFlightNo\":\"\",\"cabinClass\":\"Y\",\"stopCities\":\"\",\"baggage\":{\"pc\":2,\"weight\":23,\"isAllWeight\":true},\"depTime\":\"201712241830\",\"cabin\":\"Q\",\"depAirport\":\"LAX\",\"seatCount\":\"9\",\"flightNumber\":\"AA6007\",\"carrier\":\"AA\",\"codeShare\":false,\"aircraftCode\":\"E75\",\"arrAirport\":\"SFO\",\"operatingCarrier\":\"AA\",\"arrTime\":\"201712241955\"},{\"operatingFlightNo\":\"\",\"cabinClass\":\"Y\",\"stopCities\":\"\",\"baggage\":{\"pc\":2,\"weight\":23,\"isAllWeight\":true},\"depTime\":\"201712242250\",\"cabin\":\"Q\",\"depAirport\":\"SFO\",\"seatCount\":\"1\",\"flightNumber\":\"AA8931\",\"carrier\":\"AA\",\"codeShare\":true,\"aircraftCode\":\"77W\",\"arrAirport\":\"HKG\",\"operatingCarrier\":\"CX\",\"arrTime\":\"201712260600\"}]},\"ds\":\"ABUS_F\",\"queryCondition\":{\"adultNum\":1,\"adultNumber\":1,\"childNum\":0,\"childNumber\":0,\"cid\":\"alitriplvxing20171030\",\"comparePrice\":0,\"debug\":0,\"fromCity\":\"HKG\",\"fromDate\":\"20171124\",\"fromDate_Len10\":\"2017-11-24\",\"guid\":\"10282285985426630\",\"infantNumber\":0,\"ipccs\":[\"600058\"],\"isFusiMillis\":false,\"otaName\":\"alitrip\",\"retDate\":\"20171224\",\"retDate_Len10\":\"2017-12-24\",\"searchTimeout\":15000,\"test\":1,\"toCity\":\"LAX\",\"tradeId\":0,\"tripType\":2,\"userName\":\"alitriplvxing20171030\"},\"ipcc\":\"3YCD\",\"guid\":\"10282285985426630\",\"dsipcc\":\"ABUS_F@3YCD\",\"marriageGrps\":\"O,O,O,I\",\"accountcode\":\"\"}";
		JSONObject json= JSONObject.parseObject(s);
		
		
		JSONObject t = json.getJSONObject("travelInfo");
		
		JSONArray pi = t.getJSONArray("priceInfo");
		
		JSONObject t1 = json.getJSONObject("queryCondition");
		
		PBInnerDataEntity.PBInnerData.Builder builder = PBInnerDataEntity.PBInnerData.newBuilder();
		builder.setBasic(t.getString("basic"));
		
		StringBuilder pis = new StringBuilder();
		for (Object object : pi) {
			
			JSONObject ii = (JSONObject) object;
			if(pis.length() > 0){
				pis.append("/");
			}
			pis.append(ii.getDoubleValue("baseFare")).append("#")
			.append(ii.getDoubleValue("tax")).append("#")
			.append(ii.getIntValue("quantity")).append("#")
			.append(ii.getString("passengerTypeName")).append("#")
			.append(ii.getString("currencyCode")).append("#")
			.append(ii.getIntValue("priceType")).append("#")
			.append(ii.getString("baggage")).append("#")
			.append(ii.getDoubleValue("rebatePer")).append("#")
			.append(ii.getDoubleValue("rebateAmount")).append("#")
			.append(ii.getDoubleValue("ticketPrice")).append("#")
			.append(ii.getString("ticketPriceRule")).append("#")
			.append(ii.getString("rebateCurrency")).append("#")
			.append(ii.getDoubleValue("segmentPrice")).append("#")
			.append(ii.getDoubleValue("fareExchangeRate")).append("#")
			.append(ii.getDoubleValue("rebateExchangeRate")).append("#")
			.append(ii.getDoubleValue("rmbFare")).append("#")
			.append(ii.getDoubleValue("rmbTax")).append("#")
			.append(ii.getDoubleValue("afterRebateFare")).append("#")
			.append(ii.getDoubleValue("rmbRebateAmount")).append("#")
			.append(ii.getDoubleValue("rmbTicketPrice")).append("#")
			.append(ii.getDoubleValue("serviceFee")).append("#")
			.append(ii.get("serviceFeeCurrency")).append("#")
			.append(ii.getDoubleValue("serviceFeeRate")).append("#")
			.append(ii.getDoubleValue("serviceFeeRmb")).append("#")
			.append(ii.getString("policyId")).append("#")
			.append(ii.get("priceProfitCidId")).append("#")
			.append(ii.getDoubleValue("firmAmountCid")).append("#")
			.append(ii.getDoubleValue("percentAmountCid")).append("#")
			.append(ii.get("priceProfitHxId")).append("#")
			.append(ii.get("firmAmountHx")).append("#")
			.append(ii.get("percentAmountHx")).append("#")
			.append(ii.get("priceProfitBizHxId")).append("#")
			.append(ii.getDoubleValue("bizFirmAmount")).append("#")
			.append(ii.getDoubleValue("bizPercentAmount")).append("#")
			.append(ii.getDoubleValue("taxFirmAmount")).append("#")
			.append(ii.getDoubleValue("taxPercentAmount")).append("#")
			.append(ii.getIntValue("openAutoAdjustTax")).append("#")
			.append(ii.getDoubleValue("cidAdjustAmt")).append("#")
			.append(ii.getDoubleValue("lastRmbFare")).append("#")
			.append(ii.getDoubleValue("lastTaxFare")).append("#")
			.append(ii.getIntValue("keepDigit")).append("#")
			.append(ii.getString("ticketSettleRule"));
		}
for (Object object : pi) {
			
			JSONObject ii = (JSONObject) object;
			if(pis.length() > 0){
				pis.append("/");
			}
			pis.append(ii.getDoubleValue("baseFare")).append("#")
			.append(ii.getDoubleValue("tax")).append("#")
			.append(ii.getIntValue("quantity")).append("#")
			.append(ii.getString("passengerTypeName")).append("#")
			.append(ii.getString("currencyCode")).append("#")
			.append(ii.getIntValue("priceType")).append("#")
			.append(ii.getString("baggage")).append("#")
			.append(ii.getDoubleValue("rebatePer")).append("#")
			.append(ii.getDoubleValue("rebateAmount")).append("#")
			.append(ii.getDoubleValue("ticketPrice")).append("#")
			.append(ii.getString("ticketPriceRule")).append("#")
			.append(ii.getString("rebateCurrency")).append("#")
			.append(ii.getDoubleValue("segmentPrice")).append("#")
			.append(ii.getDoubleValue("fareExchangeRate")).append("#")
			.append(ii.getDoubleValue("rebateExchangeRate")).append("#")
			.append(ii.getDoubleValue("rmbFare")).append("#")
			.append(ii.getDoubleValue("rmbTax")).append("#")
			.append(ii.getDoubleValue("afterRebateFare")).append("#")
			.append(ii.getDoubleValue("rmbRebateAmount")).append("#")
			.append(ii.getDoubleValue("rmbTicketPrice")).append("#")
			.append(ii.getDoubleValue("serviceFee")).append("#")
			.append(ii.get("serviceFeeCurrency")).append("#")
			.append(ii.getDoubleValue("serviceFeeRate")).append("#")
			.append(ii.getDoubleValue("serviceFeeRmb")).append("#")
			.append(ii.getString("policyId")).append("#")
			.append(ii.get("priceProfitCidId")).append("#")
			.append(ii.getDoubleValue("firmAmountCid")).append("#")
			.append(ii.getDoubleValue("percentAmountCid")).append("#")
			.append(ii.get("priceProfitHxId")).append("#")
			.append(ii.get("firmAmountHx")).append("#")
			.append(ii.get("percentAmountHx")).append("#")
			.append(ii.get("priceProfitBizHxId")).append("#")
			.append(ii.getDoubleValue("bizFirmAmount")).append("#")
			.append(ii.getDoubleValue("bizPercentAmount")).append("#")
			.append(ii.getDoubleValue("taxFirmAmount")).append("#")
			.append(ii.getDoubleValue("taxPercentAmount")).append("#")
			.append(ii.getIntValue("openAutoAdjustTax")).append("#")
			.append(ii.getDoubleValue("cidAdjustAmt")).append("#")
			.append(ii.getDoubleValue("lastRmbFare")).append("#")
			.append(ii.getDoubleValue("lastTaxFare")).append("#")
			.append(ii.getIntValue("keepDigit")).append("#")
			.append(ii.getString("ticketSettleRule"));
		}
		
		builder.setPriceInfo(pis.toString());
		builder.setProductType("default");
		
		StringBuilder qc = new StringBuilder()
				.append(t1.getString("fromCity")).append("#")
				.append(t1.getString("toCity")).append("#")
				.append(t1.getString("fromDate")).append("#")
				.append(t1.getString("retDate")).append("#")
				.append(t1.getIntValue("tripType")).append("#")
				.append(t1.getIntValue("adultNumber")).append("#")
				.append(t1.getIntValue("childNumber")).append("#")
				.append(t1.getString("cid"));
		
		builder.setQueryCondition(qc.toString());
//		builder.setQueryResult(GzipUtil.compress(json.getString("queryResult")));
//		builder.setQueryResult(json.getString("queryResult"));
		builder.setQueryResult("");
		builder.setVendorId("600040");
		
		PBInnerDataEntity.PBInnerData travelInfo = builder.build();
		
		byte[] b = travelInfo.toByteArray();
		
		System.out.println(b.length);
		
		String s1 = Base64.encodeToString(b);
		
//		String s1 = bytesToHexString(b);
		
//		String s1 = new String(b);
		
		System.out.println(s1.length());
		
		System.out.println(s1);
		
		// ddd
		
		System.out.println("-------------------华丽的分割线--------------------------");
		String c = t.getString("basic")+"_"+pis.toString()+"_"+qc.toString();
		System.out.println(c.length());
		c = GzipUtil.compress(c);
		System.out.println(c.length());
		
	}
	
	static void testTravelInfo() throws InvalidProtocolBufferException{
		String s = "{\"routecodes\":\"HKG-PEK,PEK-LAX,LAX-SFO,SFO-HKG;CA,AA,AA,AA;CA6606,AA180,AA6007,AA8931;2017-11-24 12:00:00/2017-11-24 15:20:00,2017-11-24 18:30:00/2017-11-24 15:15:00,2017-12-24 18:30:00/2017-12-24 19:55:00,2017-12-24 22:50:00/2017-12-26 06:00:00;V,N,Q,Q\",\"dsid\":\"ABUS_F-164\",\"travelInfo\":{\"accountcode\":\"\",\"adjustprice\":0,\"basic\":\"ABUS_F-164#164#Economy#E#0^ ^ ^CA^CX^6606^2017-11-24 12:00:00^2017-11-24 15:20:00^9^200^HKG^PEK^333^ ^V^ ^190^0^ ^true^1^3^8^8^O@^0^ ^ ^AA^AA^180^2017-11-24 18:30:00^2017-11-24 15:15:00^9^765^PEK^LAX^788^ ^N^DS^175^0^ ^false^2^B^8^-8^O@^1^ ^ ^AA^AA^6007^2017-12-24 18:30:00^2017-12-24 19:55:00^9^85^LAX^SFO^E75^ ^Q^ ^175^0^ ^false^0^2^-8^-8^O@^1^ ^ ^AA^CX^8931^2017-12-24 22:50:00^2017-12-26 06:00:00^1^910^SFO^HKG^77W^ ^Q^BD^0^0^ ^true^I^1^-8^8^I#4910^2191^1^ADT^HKD^0^0,1-2P;2,3-2P##HKG-PEK,PEK-LAX,LAX-SFO,SFO-HKG;CA,AA,AA,AA;CA6606,AA180,AA6007,AA8931;2017-11-24 12:00:00/2017-11-24 15:20:00,2017-11-24 18:30:00/2017-11-24 15:15:00,2017-12-24 18:30:00/2017-12-24 19:55:00,2017-12-24 22:50:00/2017-12-26 06:00:00;V,N,Q,Q#private#roundTrip#ABUS_F#HKG-LAX#AA#7101#4910#7101#4910#0#3YCD#NLE08QN1%2FHK07,NLE08QN1%2FHK07;QLECX2O3%2FHK07,QLECX2O3%2FHK07#AA#private,private;private,private#\",\"ds\":\"ABUS_F\",\"dsid\":\"ABUS_F-164\",\"farebasis\":\"NLE08QN1%2FHK07,NLE08QN1%2FHK07;QLECX2O3%2FHK07,QLECX2O3%2FHK07\",\"faretype\":\"private,private;private,private\",\"flightClass\":\"Economy\",\"flightClassCode\":\"E\",\"flightOption\":\"roundTrip\",\"flightSegments\":[{\"arrivalAirport\":\"PEK\",\"arrivalCityCode\":\" \",\"arrivalTime\":\"2017-11-24 15:20:00\",\"arrivalTimeZone\":\"8\",\"bookingCode\":\"V\",\"departAirport\":\"HKG\",\"departCityCode\":\" \",\"departureTime\":\"2017-11-24 12:00:00\",\"departureTimeZone\":\"8\",\"eQuipType\":\"333\",\"eTicket\":\" \",\"elapseTime\":200,\"flightNumber\":\"6606\",\"fromterminal\":\"1\",\"jingting\":\" \",\"marketingCarrier\":\"CA\",\"marriageGrp\":\"O\",\"meal\":\" \",\"operatingCarrier\":\"CX\",\"originDestinationOption\":\"0\",\"seatsRemain\":9,\"sharecode\":true,\"stayTime\":190,\"stopQuantity\":\"0\",\"toterminal\":\"3\"},{\"arrivalAirport\":\"LAX\",\"arrivalCityCode\":\" \",\"arrivalTime\":\"2017-11-24 15:15:00\",\"arrivalTimeZone\":\"-8\",\"bookingCode\":\"N\",\"departAirport\":\"PEK\",\"departCityCode\":\" \",\"departureTime\":\"2017-11-24 18:30:00\",\"departureTimeZone\":\"8\",\"eQuipType\":\"788\",\"eTicket\":\" \",\"elapseTime\":765,\"flightNumber\":\"180\",\"fromterminal\":\"2\",\"jingting\":\" \",\"marketingCarrier\":\"AA\",\"marriageGrp\":\"O\",\"meal\":\"DS\",\"operatingCarrier\":\"AA\",\"originDestinationOption\":\"0\",\"seatsRemain\":9,\"sharecode\":false,\"stayTime\":175,\"stopQuantity\":\"0\",\"toterminal\":\"B\"},{\"arrivalAirport\":\"SFO\",\"arrivalCityCode\":\" \",\"arrivalTime\":\"2017-12-24 19:55:00\",\"arrivalTimeZone\":\"-8\",\"bookingCode\":\"Q\",\"departAirport\":\"LAX\",\"departCityCode\":\" \",\"departureTime\":\"2017-12-24 18:30:00\",\"departureTimeZone\":\"-8\",\"eQuipType\":\"E75\",\"eTicket\":\" \",\"elapseTime\":85,\"flightNumber\":\"6007\",\"fromterminal\":\"0\",\"jingting\":\" \",\"marketingCarrier\":\"AA\",\"marriageGrp\":\"O\",\"meal\":\" \",\"operatingCarrier\":\"AA\",\"originDestinationOption\":\"1\",\"seatsRemain\":9,\"sharecode\":false,\"stayTime\":175,\"stopQuantity\":\"0\",\"toterminal\":\"2\"},{\"arrivalAirport\":\"HKG\",\"arrivalCityCode\":\" \",\"arrivalTime\":\"2017-12-26 06:00:00\",\"arrivalTimeZone\":\"8\",\"bookingCode\":\"Q\",\"departAirport\":\"SFO\",\"departCityCode\":\" \",\"departureTime\":\"2017-12-24 22:50:00\",\"departureTimeZone\":\"-8\",\"eQuipType\":\"77W\",\"eTicket\":\" \",\"elapseTime\":910,\"flightNumber\":\"8931\",\"fromterminal\":\"I\",\"jingting\":\" \",\"marketingCarrier\":\"AA\",\"marriageGrp\":\"I\",\"meal\":\"BD\",\"operatingCarrier\":\"CX\",\"originDestinationOption\":\"1\",\"seatsRemain\":1,\"sharecode\":true,\"stayTime\":0,\"stopQuantity\":\"0\",\"toterminal\":\"1\"}],\"flightcode\":\"AA\",\"fromto\":\"HKG-LAX\",\"infoSource\":\"private\",\"itineraryID\":\"164\",\"marriageGrps\":\"O,O,O,I\",\"policy\":\"\",\"price\":7101.0,\"price2\":7101.0,\"priceInfo\":[{\"afterRebateFare\":4189,\"baggage\":\"0,1-2P;2,3-2P\",\"baseFare\":4910.0,\"bizFirmAmount\":0,\"bizPercentAmount\":0,\"cidAdjustAmt\":0,\"currencyCode\":\"HKD\",\"fareExchangeRate\":0.853,\"firmAmountCid\":0,\"firmAmountHx\":0,\"keepDigit\":0,\"lastRmbFare\":4189,\"lastTaxFare\":1869,\"openAutoAdjustTax\":0,\"passengerTypeName\":\"ADT\",\"percentAmountCid\":0,\"percentAmountHx\":0,\"policyId\":\"0\",\"priceType\":0,\"quantity\":1,\"rebateAmount\":0.0,\"rebateCurrency\":\"CNY\",\"rebateExchangeRate\":1.0,\"rebatePer\":0.0,\"rmbFare\":4188.2300,\"rmbRebateAmount\":0.0000,\"rmbTax\":1868.9230,\"rmbTicketPrice\":0.0000,\"segmentPrice\":0.0,\"serviceFee\":0.0,\"serviceFeeRate\":1.0,\"serviceFeeRmb\":0,\"tax\":2191.0,\"taxFirmAmount\":0,\"taxPercentAmount\":0,\"ticketPrice\":0.0,\"ticketPriceRule\":\"0\",\"ticketSettleRule\":\"1\"}],\"pricej\":{\"jsRatio\":1.0,\"adultTax\":1869.0,\"childTax\":0.0,\"adultPrice\":4189.0,\"localCurrencyCode\":\"HKD\",\"childPrice\":0.0,\"priceAddInfo\":\"\",\"rmbRatio\":0.853},\"productType\":\"default\",\"qsource\":\"3YCD\",\"routecode\":\"HKG-PEK,PEK-LAX,LAX-SFO,SFO-HKG;CA,AA,AA,AA;CA6606,AA180,AA6007,AA8931;2017-11-24 12:00:00/2017-11-24 15:20:00,2017-11-24 18:30:00/2017-11-24 15:15:00,2017-12-24 18:30:00/2017-12-24 19:55:00,2017-12-24 22:50:00/2017-12-26 06:00:00;V,N,Q,Q\",\"valid\":true,\"validatingcarrier\":\"AA\",\"ywprice\":4910.0,\"ywprice2\":4910.0},\"vendorId\":\"600058\",\"index\":146,\"queryResult\":{\"applyType\":\"0\",\"adultTax\":1869,\"suitAge\":\"\",\"data\":\"10282285985426630_146\",\"childTax\":1869,\"adultPrice\":4189,\"priceType\":\"0\",\"rule\":{\"specialNoShow\":0,\"partEndorse\":0,\"endorsement\":0,\"baggage\":\"去程：2件/回程：2件\",\"penalty\":\"100%\",\"hasRefund\":0,\"hasBaggage\":1,\"endorse\":\"\",\"hasNoShow\":0,\"hasEndorse\":0,\"noShowLimitTime\":0,\"partRefund\":0,\"refund\":\"100%-0-*\"},\"childTaxType\":\"0\",\"gvChildRule\":\"1\",\"minPassengerCount\":1,\"fromSegments\":[{\"operatingFlightNo\":\"\",\"cabinClass\":\"Y\",\"stopCities\":\"\",\"baggage\":{\"pc\":2,\"weight\":23,\"isAllWeight\":true},\"depTime\":\"201711241200\",\"cabin\":\"V\",\"depAirport\":\"HKG\",\"seatCount\":\"9\",\"flightNumber\":\"CA6606\",\"carrier\":\"CA\",\"codeShare\":true,\"aircraftCode\":\"333\",\"arrAirport\":\"PEK\",\"operatingCarrier\":\"CX\",\"arrTime\":\"201711241520\"},{\"operatingFlightNo\":\"\",\"cabinClass\":\"Y\",\"stopCities\":\"\",\"baggage\":{\"pc\":2,\"weight\":23,\"isAllWeight\":true},\"depTime\":\"201711241830\",\"cabin\":\"N\",\"depAirport\":\"PEK\",\"seatCount\":\"9\",\"flightNumber\":\"AA180\",\"carrier\":\"AA\",\"codeShare\":false,\"aircraftCode\":\"788\",\"arrAirport\":\"LAX\",\"operatingCarrier\":\"AA\",\"arrTime\":\"201711241515\"}],\"nationality\":\"\",\"maxPassengerCount\":9,\"nationalityType\":\"0\",\"invoiceType\":3,\"adultTaxType\":\"0\",\"childPrice\":4189,\"retSegments\":[{\"operatingFlightNo\":\"\",\"cabinClass\":\"Y\",\"stopCities\":\"\",\"baggage\":{\"pc\":2,\"weight\":23,\"isAllWeight\":true},\"depTime\":\"201712241830\",\"cabin\":\"Q\",\"depAirport\":\"LAX\",\"seatCount\":\"9\",\"flightNumber\":\"AA6007\",\"carrier\":\"AA\",\"codeShare\":false,\"aircraftCode\":\"E75\",\"arrAirport\":\"SFO\",\"operatingCarrier\":\"AA\",\"arrTime\":\"201712241955\"},{\"operatingFlightNo\":\"\",\"cabinClass\":\"Y\",\"stopCities\":\"\",\"baggage\":{\"pc\":2,\"weight\":23,\"isAllWeight\":true},\"depTime\":\"201712242250\",\"cabin\":\"Q\",\"depAirport\":\"SFO\",\"seatCount\":\"1\",\"flightNumber\":\"AA8931\",\"carrier\":\"AA\",\"codeShare\":true,\"aircraftCode\":\"77W\",\"arrAirport\":\"HKG\",\"operatingCarrier\":\"CX\",\"arrTime\":\"201712260600\"}]},\"ds\":\"ABUS_F\",\"queryCondition\":{\"adultNum\":1,\"adultNumber\":1,\"childNum\":0,\"childNumber\":0,\"cid\":\"alitriplvxing20171030\",\"comparePrice\":0,\"debug\":0,\"fromCity\":\"HKG\",\"fromDate\":\"20171124\",\"fromDate_Len10\":\"2017-11-24\",\"guid\":\"10282285985426630\",\"infantNumber\":0,\"ipccs\":[\"600058\"],\"isFusiMillis\":false,\"otaName\":\"alitrip\",\"retDate\":\"20171224\",\"retDate_Len10\":\"2017-12-24\",\"searchTimeout\":15000,\"test\":1,\"toCity\":\"LAX\",\"tradeId\":0,\"tripType\":2,\"userName\":\"alitriplvxing20171030\"},\"ipcc\":\"3YCD\",\"guid\":\"10282285985426630\",\"dsipcc\":\"ABUS_F@3YCD\",\"marriageGrps\":\"O,O,O,I\",\"accountcode\":\"\"}";
		JSONObject json= JSONObject.parseObject(s);
		
		JSONObject t = json.getJSONObject("travelInfo");
		
		JSONArray pi = t.getJSONArray("priceInfo");
		
		JSONObject t1 = json.getJSONObject("queryCondition");
		
		PBTravelInfoEntity.PBTravelInfo.Builder builder = PBTravelInfoEntity.PBTravelInfo.newBuilder();
		builder.setAccountcode(t.getString("accountcode"));
		builder.setDs(t.getString("ds"));
		builder.setDsid(t.getString("dsid"));
		builder.setFarebasis(t.getString("farebasis"));
		builder.setFaretype(t.getString("faretype"));
		builder.setInfoSource(t.getString("infoSource"));
		builder.setMarriageGrps(t.getString("marriageGrps"));
		builder.setPrice(t.getDoubleValue("price"));
		builder.setPrice2(t.getDoubleValue("price2"));
		builder.setQsource(t.getString("qsource"));
		builder.setRoutecode(t.getString("routecode"));
		builder.setValidatingcarrier(t.getString("validatingcarrier"));
		
		PBTravelInfoEntity.PBQueryCondition.Builder conditionBuilder = PBTravelInfoEntity.PBQueryCondition.newBuilder();
		conditionBuilder.setAdultnumber(t1.getIntValue("adultNumber"));
		conditionBuilder.setChildnumber(t1.getIntValue("childNumber"));
		conditionBuilder.setCid(t1.getString("cid"));
		conditionBuilder.setFromcity(t1.getString("fromCity"));
		conditionBuilder.setFromdate(t1.getString("fromDate"));
		conditionBuilder.setGuid(t1.getString("guid"));
		conditionBuilder.setRetdate(t1.getString("retDate"));
		conditionBuilder.setTocity(t1.getString("toCity"));
		conditionBuilder.setTriptype(t1.getIntValue("tripType"));
		conditionBuilder.setUserName(t1.getString("userName"));
		builder.setQueryCondition(conditionBuilder.build());
		
		int index = 0;
		for (Object object : pi) {
			JSONObject ii = (JSONObject) object;
			
			PBTravelInfoEntity.PBPriceInfo.Builder pBuilder = PBTravelInfoEntity.PBPriceInfo.newBuilder();
			
			pBuilder.setAfterRebateFare(ii.getDoubleValue("afterRebateFare"));
			pBuilder.setBaggage(ii.getString("baggage"));
			pBuilder.setBaseFare(ii.getDoubleValue("baseFare"));
			pBuilder.setBizFirmAmount(ii.getDoubleValue("bizFirmAmount"));
			pBuilder.setBizPercentAmount(ii.getDoubleValue("bizPercentAmount"));
			pBuilder.setCidAdjustAmt(ii.getDoubleValue("cidAdjustAmt"));
			pBuilder.setCurrencyCode(ii.getString("currencyCode"));
			pBuilder.setFareExchangeRate(ii.getDoubleValue("fareExchangeRate"));
			pBuilder.setFirmAmountCid(ii.getDoubleValue("firmAmountCide"));
			pBuilder.setFirmAmountHx(ii.getDoubleValue("firmAmountHx"));
			pBuilder.setKeepDigit(ii.getIntValue("keepDigit"));
			pBuilder.setLastRmbFare(ii.getDoubleValue("lastRmbFare"));
			pBuilder.setLastTaxFare(ii.getDoubleValue("lastTaxFare"));
			pBuilder.setOpenAutoAdjustTax(ii.getIntValue("openAutoAdjustTax"));
			pBuilder.setPassengerTypeName(ii.getString("passengerTypeName"));
			pBuilder.setPercentAmountCid(ii.getDoubleValue("percentAmountCide"));
			pBuilder.setPercentAmountHx(ii.getDoubleValue("percentAmountHx"));
			pBuilder.setPolicyId(ii.getString("policyId"));
			pBuilder.setPriceProfitBizHxId("1212");
			pBuilder.setPriceProfitCidId("121");
			pBuilder.setPriceProfitHxId("1212");
			pBuilder.setPriceType(ii.getIntValue("priceType"));
			pBuilder.setQuantity(ii.getIntValue("quantity"));
			pBuilder.setRebateAmount(ii.getDoubleValue("rebateAmount"));
			pBuilder.setRebateCurrency(ii.getString("rebateCurrency"));
			pBuilder.setRebateExchangeRate(ii.getDoubleValue("rebateExchangeRate"));
			pBuilder.setRebatePer(ii.getDoubleValue("rebatePer"));
			pBuilder.setRmbFare(ii.getDoubleValue("rmbFare"));
			pBuilder.setRmbRebateAmount(ii.getDoubleValue("rmbRebateAmount"));
			pBuilder.setRmbTax(ii.getDoubleValue("rmbTax"));
			pBuilder.setRmbTicketPrice(ii.getDoubleValue("rmbTicketPrice"));
			pBuilder.setSegmentPrice(ii.getDoubleValue("segmentPrice"));
			pBuilder.setServiceFee(ii.getDoubleValue("serviceFee"));
			pBuilder.setServiceFeeCurrency("111");
			pBuilder.setServiceFeeRate(ii.getDoubleValue("serviceFeeRate"));
			pBuilder.setServiceFeeRmb(ii.getDoubleValue("serviceFeeRmb"));
			pBuilder.setTax(ii.getDoubleValue("tax"));
			pBuilder.setTaxFirmAmount(ii.getDoubleValue("taxFirmAmount"));
			pBuilder.setTaxPercentAmount(ii.getDoubleValue("taxPercentAmount"));
			pBuilder.setTicketPrice(ii.getDoubleValue("ticketPrice"));
			pBuilder.setTicketPriceRule(ii.getString("ticketPriceRule"));
			pBuilder.setTicketSettleRule(ii.getString("ticketSettleRule"));
			
			builder.addPriceInfos(index, pBuilder.build());
			
			index ++;
		}
		
		PBTravelInfoEntity.PBPriceInfo.Builder pBuilder = PBTravelInfoEntity.PBPriceInfo.newBuilder();
		
		pBuilder.setAfterRebateFare(11);
		pBuilder.setBaggage("12121");
		pBuilder.setBaseFare(11);
		pBuilder.setBizFirmAmount(11);
		pBuilder.setBizPercentAmount(11);
		pBuilder.setCidAdjustAmt(11);
		pBuilder.setCurrencyCode("ccc");
		pBuilder.setFareExchangeRate(11);
		pBuilder.setFirmAmountCid(11);
		pBuilder.setFirmAmountHx(11);
		pBuilder.setKeepDigit(1);
		pBuilder.setLastRmbFare(11);
		pBuilder.setLastTaxFare(11);
		pBuilder.setOpenAutoAdjustTax(11);
		pBuilder.setPassengerTypeName("add");
		pBuilder.setPercentAmountCid(11);
		pBuilder.setPercentAmountHx(11);
		pBuilder.setPolicyId("1");
		pBuilder.setPriceProfitBizHxId("1212");
		pBuilder.setPriceProfitCidId("121");
		pBuilder.setPriceProfitHxId("1212");
		pBuilder.setPriceType(1);
		pBuilder.setQuantity(11);
		pBuilder.setRebateAmount(11);
		pBuilder.setRebateCurrency("ccc");
		pBuilder.setRebateExchangeRate(11);
		pBuilder.setRebatePer(11);
		pBuilder.setRmbFare(11);
		pBuilder.setRmbRebateAmount(11);
		pBuilder.setRmbTax(11);
		pBuilder.setRmbTicketPrice(11);
		pBuilder.setSegmentPrice(11);
		pBuilder.setServiceFee(11);
		pBuilder.setServiceFeeCurrency("111");
		pBuilder.setServiceFeeRate(11);
		pBuilder.setServiceFeeRmb(11);
		pBuilder.setTax(11);
		pBuilder.setTaxFirmAmount(11);
		pBuilder.setTaxPercentAmount(11);
		pBuilder.setTicketPrice(1);
		pBuilder.setTicketPriceRule("11");
		pBuilder.setTicketSettleRule("11");
		
		builder.addPriceInfos(index, pBuilder.build());
		
		PBTravelInfoEntity.PBTravelInfo travelInfo = builder.build();
		
		byte[] b = travelInfo.toByteArray();
		
		System.out.println(b.length);
		
		String s1 = Base64.encodeToString(b);
		
//		String s1 = bytesToHexString(b);
		
//		String s1 = new String(b);
		
		System.out.println(s1.length());
		
		System.out.println(s1);
		
//		byte[] c = Base64.decode(s1);
		
//		byte[] c = hexStringToByte(s1);
		
//		PBTravelInfoEntity.PBTravelInfo travelInfo2 = PBTravelInfoEntity.PBTravelInfo.parseFrom(c);
//		System.out.println(travelInfo2.toString());
	}
	
	static void testPerson() throws InvalidProtocolBufferException{
		PersonEntity.Person.Builder builder = PersonEntity.Person.newBuilder();
		builder.setEmail("test@qq.cpm");
		builder.setId(121212);
		builder.setName("test demo");
		PersonEntity.Person person = builder.build();
		System.out.println(person.toString());
		
		System.out.println(person.toByteString().toString());
		
		byte[] b = person.toByteArray();
		
		System.out.println(b.length);
		
		String s1 = Base64.encodeToString(b);
		System.out.println(s1);
		
		byte[] c = Base64.decode(s1);
		
		PersonEntity.Person person2 = PersonEntity.Person.parseFrom(c);
		System.out.println(person2.toString());
	}

	
	/**
	 * 字符串转成字节数组.
	 * 
	 * @param hex
	 *            要转化的字符串.
	 * @return byte[] 返回转化后的字符串.
	 */
	public static byte[] hexStringToByte(String hex) {
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	private static byte toByte(char c) {
		byte b = (byte) "0123456789ABCDEF".indexOf(c);
		return b;
	}

	/**
	 * 字节数组转成字符串.
	 * 
	 * @param String
	 *            要转化的字符串.
	 * @return 返回转化后的字节数组.
	 */
	public static final String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}
}
